package com.ltv.xiangqiplugin.models;

import java.util.Vector;

public class PositionStack {
	private Vector<String> positions;
	private int index = 0;
	private Vector<PositionStackObserver> observers;
	
	public PositionStack() {
		positions = new Vector<String>();
		observers = new Vector<PositionStackObserver>();
	}
	
	public void addPostion(String position) {
		for (int i = 0; i < index; i++) {
			positions.remove(0);
		}
		index = 0;
		positions.add(0, position);
	}
	
	public void next() {
		if (index > 0) {
			index--;
			notifyObservers();
		}
	}
	
	public void last() {
		if (index > 0) {
			index = 0;
			notifyObservers();
		}
	}
	
	public void prev() {
		if (index < positions.size() - 1) {
			index++;
			notifyObservers();
		}
	}
	
	public void first() {
		if (index < positions.size() - 1) {
			index = positions.size() - 1;
			notifyObservers();
		}
	}
	
	public void addObserver(PositionStackObserver observer) {
		observers.add(observer);
	}
	
	public void removeObserver(PositionStackObserver observer) {
		observers.remove(observer);
	}
	
	public void notifyObservers() {
		for (PositionStackObserver observer : observers) {
			observer.positionChange(positions.get(index));
		}
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getIndex() {
		return index;
	}

	public void setPositions(Vector<String> positions) {
		this.positions = positions;
	}

	public Vector<String> getPositions() {
		return positions;
	}
	
}
